<!-- 转正详情 -->
<template>
  <ContentCard>
    <template #header>
      <pageHeaderNew title="转正详情" showBack></pageHeaderNew>

      <div>
        <div v-for="(item, index) in data" :key="index">
          <div :id="item.collectionId">
            <div class="title-box">
              <div class="title">
                <div
                  :style="{
                    marginLeft: '20px'
                  }"
                >
                  {{ item.collectionName }}
                </div>
                <div>
                  <a-tooltip content="编辑信息">
                    <icon-font
                      v-show="currCollectionId !== item.collectionId"
                      :style="{
                        cursor: 'pointer'
                      }"
                      type="icon-edit"
                      :size="36"
                      @click="edit(item.collectionId)"
                    ></icon-font>
                  </a-tooltip>
                </div>
              </div>
            </div>

            <a-form :model="item" ref="formRef" autoLabelWidth>
              <a-row :gutter="16">
                <a-col
                  v-for="item2 in item.controlItems"
                  :key="item2.id"
                  :span="item2.span"
                >
                  <FormItem
                    :name="item2.ctlCnName"
                    :componentType="item2.ctlComponentType"
                    :code="item2.ctlCode"
                    :props="item2.props"
                    v-model:values="item2.ctlValue"
                    :required="false"
                    :validateRules="
                      checkEnums.includes(item2.ctlCode) ? validate : null
                    "
                    :disabled="
                      currCollectionId !== item.collectionId ||
                      forbidEditEnum.includes(item2.ctlCode)
                    "
                  ></FormItem>
                </a-col>
              </a-row>
            </a-form>
          </div>
        </div>
      </div>
    </template>
  </ContentCard>
</template>

<script lang="ts" setup>
import { GetBusinessModalData } from '@/apis/controltotal';
const route = useRoute();
const id = route.query.id;
const title = '转正详情';

// 拼用来数据的数据
const data = reactive([
  {
    collectionId: undefined,
    collectionName: '',
    controlItems: [] as any
  }
]);
const orginData = ref(); // 原始数据
const getData = async () => {
  try {
    const [businessModalData] = await Promise.all([
      GetBusinessModalData({
        funcCode: 'SER_FUNC9479BA087B5B46F2A6547ACE4D69DE69'
      })
    ]);
    let collectionIdArr: any = []; // 储存有多少不同的collectionId

    orginData.value.forEach((item: any) => {
      /* 储存唯一的collectionId */
      if (!collectionIdArr.includes(item.id)) collectionIdArr.push(item.id);
      item.controlModel.forEach((item2: any) => {
        let value = item2.ctlValue.ctlValue;
        if (
          // 组织名称
          item2.ctlCode === 'C30BBF36A5AF541F7A2657671DEE2EDD6'
        )
          origin.name = value;
        else if (
          // 组织编码
          item2.ctlCode === 'CB1AAC9E508714223BA4D1596FE690877'
        )
          origin.code = value;
        else if (
          // 上级组织
          item2.ctlCode === 'C073F4F41C46D4CA3AF3BEC1F51A8F410'
        )
          origin.superiorId = +value;
      });
    });

    /* 每次先将数组清空再赋一个空对象然后传入唯一的collectionId */
    data.length = 0;

    collectionIdArr.forEach((item: any, index: any) => {
      data.push({
        collectionId: undefined,
        collectionName: '',
        controlItems: []
      });
      data[index].collectionId = item;
    });

    // 循环赋值
    data.forEach((item1: any) => {
      orginData.value.forEach((item2: any) => {
        item2.controlModel.forEach((item3: any) => {
          let ctlCode = item3.ctlCode,
            ctlValue = item3.ctlValue.ctlValue;
          if (item1.collectionId === item2.id) {
            if (item1.collectionName === '')
              item1.collectionName = item2.cnName;
            item1.controlItems.push({
              controlId: item3.id,
              collectionId: item2.collectionId,
              orderNo: item2.orderNo,

              // 如果ctlCode是以下的值就要转为Number类型的值
              ctlValue:
                isNaN(+ctlValue) && ctlValue !== 'undefined'
                  ? ctlValue
                  : ctlValue === ''
                  ? ''
                  : ctlValue == 'undefined'
                  ? undefined
                  : +ctlValue,
              id: item3.ctlValue.id,
              ctlCnName: item3.ctlCnName,
              ctlCode: ctlCode,
              code: ctlCode,
              ctlComponentType: item3.ctlComponentType,
              props: JSON.parse(item3.props),
              required: item3.required,
              span: item3.span
            });
          }
        });
      });
    });
  } catch (error: unknown) {
    console.log(error);
  }
};
getData();
</script>

<style lang="less" scoped>
.container {
  padding: 10px 20px;
}
</style>
